﻿/*
 * This file is part of CommunitySettlers.
 *
 * CommunitySettlers is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.

 * CommunitySettlers is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with CommunitySettlers.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * Author: Christoph Husse
 * 
 * Also checkout our homepage: http://communitysettlers.codeplex.com/
 */
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Drawing;

namespace SettlersEngine
{

    internal partial class RuntimeMap
    {
        public Int32 ScreenWidth { get; private set; }
        public Int32 ScreenHeight { get; private set; }
        private const Int32 SCREEN_BORDER = 5;

        public Rectangle ScreenViewport { get; set; }
        public Point ScreenPosition { get; set; }

        public void MoveScreen(Point inTopLeftCorner)
        {
            int x = inTopLeftCorner.X, y = inTopLeftCorner.Y;

            if (x < 0) x = 0;
            if (x >= Width) x = Width - 1;
            if (y < 0) y = 0;
            if (y >= Height) y = Height - 1;

            ScreenPosition = new Point(x, y);

            UpdateScreen();
        }

        public void MoveScreen(int inTopLeftCornerX, int inTopLeftCornerY)
        {
            MoveScreen(new Point(inTopLeftCornerX, inTopLeftCornerY));

            UpdateScreen();
        }

        public void SetScreenSize(int inGridWidth, int inGridHeight)
        {
            ScreenWidth = inGridWidth;
            ScreenHeight = inGridHeight;
        }

        internal void UpdateScreen()
        {
            lock (m_ChangeLock)
            {
                Rectangle oldView = ScreenViewport;
                Int32 newLeft = Math.Min(Math.Max(0, ScreenPosition.X - SCREEN_BORDER), Width - 1);
                Int32 newTop = Math.Min(Math.Max(0, ScreenPosition.Y - SCREEN_BORDER), Height - 1);
                Int32 newRight = Math.Min(Width-1, newLeft + ScreenWidth + SCREEN_BORDER * 2);
                Int32 newBottom = Math.Min(Height-1, newTop + ScreenHeight + SCREEN_BORDER * 2);

                Rectangle newView = new Rectangle(
                    newLeft,
                    newTop,
                    newRight - newLeft,
                    newBottom - newTop);

                // add objects of new view
                foreach (IPositionProvider obj in Grid.Buildings)
                {
                   // if (!newView.Contains(obj.Position))
                   //     obj.Hide();
                    //else
                        obj.Show();
                }

                ScreenViewport = newView;
            }
        }
    }
}
